package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelContent;
import cn.wolfcode.trip.base.mapper.PageViewMapper;
import cn.wolfcode.trip.base.mapper.TravelContentMapper;
import cn.wolfcode.trip.base.mapper.TravelMapper;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ITravelService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author by mycp
 * @Classname TravelServiceImpl
 * @Description TODO
 * @Date 2018/11/4 12:54
 */
@Service
public class TravelServiceImpl implements ITravelService {
    @Autowired
    private TravelContentMapper travelContentMapper;
    @Autowired
    private TravelMapper travelMapper;
    @Autowired
    private PageViewMapper pageViewMapper;
    @Override
    public PageInfo queryTravel(TravelQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<Travel> list = travelMapper.queryForList(qo);
        return new PageInfo(list);
    }

    @Override
    public void saveOrUpdate(Travel entity) {
        // 手动设置创建时间 / 最后更新时间
        Date date = new Date();
        entity.setCreateTime(date);
        entity.setLastUpdateTime(date);
        if (entity.getId() == null){
            // 保存游记
            travelMapper.insert(entity);
            // 同时需要保存游记富文本内容, 先获取entity保存以后自动生成的主键设置到富文本中, 在进行保存
            TravelContent travelContent = entity.getTravelContent();
            travelContent.setId(entity.getId());
            travelContentMapper.insert(travelContent);
        } else {

            // 同时需要设置最后更新时间
            entity.setLastUpdateTime(new Date());
            // 更新游记
            travelMapper.updateByPrimaryKey(entity);
            // 同步更新游记的内容
            TravelContent travelContent = entity.getTravelContent();
            // 设置游记和游记内容的主键关联
            travelContent.setId(entity.getId());
            travelContentMapper.updateByPrimaryKey(travelContent);
        }
    }

    @Override
    public Travel get(Long id) {
        // 先根据id查询游记
        Travel travel = travelMapper.selectByPrimaryKey(id);
        // 再根据游记的id查询游记内容
        TravelContent travelContent = travelContentMapper.selectByPrimaryKey(id);
        // 将游记内容设置到游记当中
        travel.setTravelContent(travelContent);
        return travel;
    }

    @Override
    public TravelContent getTravelContentById(Long travelId) {
        return travelContentMapper.selectByPrimaryKey(travelId);
    }

    @Override
    public void changeTravelState(Long id, Integer state) {
        // 判断是发布 / 拒绝类型的操作
        if (state == 2){
            // 审核通过, 则需要设置发布时间
            travelMapper.changeTravelState(id, state, new Date());
        } else {
            // 拒绝操作
            travelMapper.changeTravelState(id, state, null);
        }
    }

    @Override
    public PageInfo queryTravelForApp(TravelQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<Travel> list = travelMapper.queryForListForApp(qo);
        return new PageInfo(list);
    }

    @Override
    public Integer count(Long authorId) {
        Integer count = travelMapper.count(authorId);
        return count;
    }

    @Override
    public List<Travel> getTravel(Long strategyId) {
        List<Travel> travels = travelMapper.selectTravelsByStrategyId(strategyId);
        return travels;
    }
}
